package com.ykt.converter;

import com.ykt.common.constant.ParamNames;
import com.ykt.common.utils.DateUtil;
import com.ykt.common.utils.ModelUtil;
import com.ykt.model.Order;
import com.ykt.model.OrderItem;
import com.ykt.vo.OrderItemVo;
import com.ykt.vo.OrderVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: long.hua
 * @date: 2016-02-17 15:47
 * @since 1.0
 */
public class OrderConvert {


    public static OrderVo convert(Order entity) {
        if (entity == null) {
            return null;
        }

        OrderVo vo = new OrderVo();
        BaseConverter.convert(vo, entity);
        vo.setOrderNo(entity.getStr(Order.order_no));
        vo.setOrderPno(entity.getStr(Order.order_pno));
        vo.setOrderType(entity.getStr(Order.order_type));
        vo.setAmount(ModelUtil.d(entity, Order.amount));
        vo.setPayAmount(ModelUtil.d(entity, Order.pay_amount));
        vo.setAgentFee(ModelUtil.d(entity, Order.agent_fee));
        vo.setStatus(entity.getStr(Order.status));
        vo.setServiceStatus(entity.getStr(Order.service_status));
        vo.setShopId(entity.getInt(Order.shop_id));
        vo.setShopName(entity.getStr(ParamNames.shopName));
        vo.setUserId(entity.getInt(Order.user_id));
        vo.setUserName(entity.getStr(ParamNames.userName));
        vo.setReceiptName(entity.getStr(Order.receipt_name));
        vo.setReceiptPhone(entity.getStr(Order.receipt_phone));
        vo.setAddress(entity.getStr(Order.address));
        vo.setRemarks(entity.getStr(Order.remarks));
        vo.setCommunityName(entity.getStr(Order.community_name));
        vo.setCommunityAddress(entity.getStr(Order.community_address));
        vo.setCommunityContactsName(entity.getStr(Order.community_contacts_name));
        vo.setCommunityContactsPhone(entity.getStr(Order.community_contacts_phone));
        vo.setPayTime(DateUtil.ymdhms2(entity.getDate(Order.pay_time)));
        vo.setPaySerialNo(entity.getStr(Order.pay_serial_no));
        vo.setCreatedTime(DateUtil.ymdhms2(entity.getDate(Order.created_time)));
        vo.setDeliveryTime(entity.getStr(Order.delivery_time));

        return vo;
    }

    public static List<OrderVo> convertOrderList(List<Order> models) {
        if (models == null || models.isEmpty()) {
            return null;
        }

        List<OrderVo> vos = new ArrayList<>(models.size());

        for (Order entity : models) {
            if (entity != null) {
                vos.add(convert(entity));
            }
        }

        return vos;
    }

    public static OrderItemVo convert(OrderItem model) {
        if (model == null) {
            return null;
        }

        OrderItemVo vo = new OrderItemVo();
        vo.setId(model.getInt(OrderItem.id));
        vo.setGoodsId(model.getInt(OrderItem.goods_id));
        vo.setGoodsName(model.getStr(OrderItem.goods_name));
        vo.setGoodsPrice(ModelUtil.d(model, OrderItem.goods_price));
        vo.setGoodsCount(model.getInt(OrderItem.goods_count));
        vo.setGoodsUnit(model.getStr(OrderItem.goods_unit));
        vo.setGoodsAmount(ModelUtil.d(model, OrderItem.goods_amount));

        return vo;
    }

    public static List<OrderItemVo> convertOderItemList(List<OrderItem> models) {
        if (models == null || models.isEmpty()) {
            return null;
        }

        List<OrderItemVo> vos = new ArrayList<>(models.size());

        for (OrderItem entity : models) {
            if (entity != null) {
                vos.add(convert(entity));
            }
        }

        return vos;
    }

}
